package com.yc.exam.biz;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yc.exam.bean.Paper;
import com.yc.exam.bean.PaperDetail;
import com.yc.exam.mapper.PaperDetailMapper;
import com.yc.exam.mapper.PaperMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/8/15
 * Email haijunzhou@hnit.edu.cn
 */
@Service
public class PaperBiz {

    @Autowired
    PaperMapper paperMapper;
    @Autowired
    PaperDetailMapper detailMapper;

    @Transactional(rollbackFor = BizExeption.class)
    public void create(Paper paper) throws BizExeption{

        //  考卷同名验证
        LambdaQueryWrapper<Paper> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Paper::getTitle,paper.getTitle());
        Paper paper1 = paperMapper.selectOne(lqw);
        if (paper1 == null) {
            System.out.println("paper1 = " + paper);

        paperMapper.insert(paper);

        for (PaperDetail detail : paper.getDetails()) {
            detail.setPid(paper.getId());
            detailMapper.insert(detail);
        }
        }else {
            throw new BizExeption("该试卷名已存在","paper");
        }


    }

}
